package com.epam.tcs.actions.pages;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.epam.tcs.actions.BreadCrumb;
import com.epam.tcs.dao.factory.DAO;
import com.epam.tcs.dao.hibernate.entities.Group;
import com.epam.tcs.dao.hibernate.entities.Program;

public class GroupPage extends PageAbstract {

	@Override
	public void doExec(HttpServletRequest req, HttpServletResponse res) {

		String target = req.getRequestURI().substring(
				req.getContextPath().length());

		String[] tarr = target.split("/");
		BreadCrumb.add(req, res, "Группы", "/tcs/group");
		try {
			if ((tarr.length == 4) && tarr[3].equals("add")) {
				addGroup(req, res);
			} else if ((tarr.length == 4) && tarr[3].equals("del")) {
				delGroup(req, res);
			} else if ((tarr.length == 4) && tarr[3].equals("edit")) {
				editGroup(req, res);
			} else {
				listGroups(req, res);
			}
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void editGroup(HttpServletRequest req, HttpServletResponse res) {
		// TODO Auto-generated method stub

	}

	private void delGroup(HttpServletRequest req, HttpServletResponse res)
			throws IOException, ServletException {
		DAO<Group> group = dao.getGroupDAO();
		String message = "";
		for (Enumeration<String> e = req.getParameterNames(); e
				.hasMoreElements();) {
			String[] s = ((String) e.nextElement()).split("_");
			Group group_element = (Group) group
					.getElement(Long.parseLong(s[2]));
			if (group_element.getStudents().size() > 0) {
				message += "Группа \"" + group_element.getName()
						+ "\" не может быть удалена, так как в ней имеются "
						+ group_element.getStudents().size()
						+ " студента(-ов).";
			} else {
				message += "Удалена группа \"" + group_element.getName()
						+ "\". ";
				group.deleteElement(group.getElement(Long.parseLong(s[2])));
			}
		}
		req.setAttribute("widget", "group");
		req.setAttribute("message", message);
		req.getSession().setAttribute("listGroups", group.getAllElements());
		outputPage("group/group.jsp", req, res);
	}

	private void addGroup(HttpServletRequest req, HttpServletResponse res)
			throws IOException, ServletException {
		BreadCrumb.add(req, res, "Добавить группу", "/tcs/group/add");
		DAO<Program> program = dao.getProgramDAO();
		if (req.getMethod() == "POST" && this.validateGroupForm(req)) {
			DAO<Group> group = dao.getGroupDAO();
			Group g = new Group();
			g.setName(req.getParameter("caption"));
			// g.setStatus(req.getParameter("status"));
			g.setProgram((Program) program.getElement(Long.parseLong(req
					.getParameter("program"))));

			if (group.addElement(g) != 0L) {
				req.setAttribute("message", "Группа \"" + g.getName()
						+ "\" успешно добавлена.");
			}
		}
		req.getSession().setAttribute("listPrograms", program.getAllElements());
		req.setAttribute("widget", "add_group");
		outputPage("group/group_form.jsp", req, res);

	}

	private void listGroups(HttpServletRequest req, HttpServletResponse res)
			throws IOException, ServletException {
		DAO<Group> group = dao.getGroupDAO();
		req.setAttribute("widget", "group");
		req.getSession().setAttribute("listGroups", group.getAllElements());
		outputPage("group/group.jsp", req, res);
	}

	private boolean validateGroupForm(HttpServletRequest req) {
		// TODO Auto-generated method stub
		return true;
	}

}
